/*
 * 所有module节点的基类
 */
ESTDesigner.module.BaseModule = draw2d.shape.basic.Rectangle.extend({
    init : function(attr, setter, getter) {
        // additional attribute
        this.name = null;
        this.iconPath = null;// icon path
        this.listeners = new draw2d.util.ArrayList();
        this.type = null;
        this.title = null;
        this.documentation=null;
        this.asynchronous=null;
        this.contextMenuHandler=null;
        this.exclusive=true;
        this.isSequential=false;
        this._loopCardinality=null;
        this._collection=null;
        this._elementVariable=null;
        this._completionCondition=null;
        this._super(
            $.extend({
                id : draw2d.util.UUID.create()
            }, attr),
            $.extend({
                type : this.setType,
                title : this.setTitle,
                iconPath : this.setIconPath
            }, setter),
            $.extend({
                type : this.getType,
                title : this.getTitle,
                iconPath : this.getIconPath
            }, getter)
        );
        this.setBackgroundColor("#93d7f3");
        this.setColor("#39b2e5");
        this.setStroke(1);
        this.setRadius(2);
        this.setDimension(50, 50);
        this.setResizeable(false);
        this.createPort("hybrid", new draw2d.layout.locator.LeftLocator());
        this.createPort("hybrid", new draw2d.layout.locator.RightLocator());
        this.createPort("hybrid", new draw2d.layout.locator.BottomLocator());
        this.createPort("hybrid", new draw2d.layout.locator.TopLocator());

        // module's icon
        this.ico = new draw2d.shape.basic.Image();
        this.ico.setDimension(50, 50);
        this.ico.path = this.iconPath == null ? "" : this.getIconPath();

        // module type label
        this.moduleTypeLabel = new draw2d.shape.basic.Label({
            bold : true,
            fontSize : 13
        });
        this.ico.add(this.moduleTypeLabel, new draw2d.layout.locator.RightLocator());
        this.moduleTypeLabel.setResizeable(false);
        this.moduleTypeLabel.setText(this.title);
        this.moduleTypeLabel.stroke = 0;
        this.moduleTypeLabel.on("contextmenu", function(emitter, event) {
            var module = emitter.getParent().getParent();
            module.onContextMenu(event.x,event.y);
        });

        // module name label
        this.moduleNameLabel = new draw2d.shape.basic.Label();
        this.moduleNameLabel.setText('Module Name');
        this.moduleNameLabel.setStroke(0);
        var editor = new draw2d.ui.LabelInplaceEditor({
            // called after the value has been set to the LabelFigure
            onCommit : $.proxy(function(value) {
                this.moduleNameLabel.getParent().name=value;
            }, this)
        });
        this.moduleNameLabel.installEditor(editor);

        this.add(this.ico, new draw2d.layout.locator.XYRelPortLocator(0, 0));
        this.add(this.moduleNameLabel, new draw2d.layout.locator.BottomLocator());
        this.ico.on("contextmenu", function(emitter, event) {
            var module = emitter.getParent();
            module.onContextMenu(event.x,event.y);
        });
        this.moduleNameLabel.on("contextmenu", function(emitter, event) {
            var module = emitter.getParent();
            module.onContextMenu(event.x,event.y);
        });
    },
    getType : function() {
        return this.iconPath;
    },
    setType : function(type) {
        this.type = type;
        this.fireEvent("change:type", {
            value : this.type
        });
        return this;
    },
    getTitle : function(){
        return this.title;
    },
    setTitle : function(title){
        this.title = title;
        this.fireEvent("change:title", {
            value : this.title
        });
        return this;
    },
    getIconPath : function() {
        return this.iconPath;
    },
    setIconPath : function(path) {
        this.iconPath = path;
        this.fireEvent("change:iconPath", {
            value : this.iconPath
        });
        return this;
    },
    setModuleName:function(name){
        this.moduleNameLabel.setText(name);
        this.name=name;
    },
    toXML : function() {
        return "";
    },
    getListener : function(id) {
        for (var i = 0; i < this.listeners.getSize(); i++) {
            var listener = this.listeners.get(i);
            if (listener.getId() === id) {
                return listener;
            }
        }
    },
    deleteListener : function(id) {
        var listener = this.getListener(id);
        this.listeners.remove(listener);
    },
    addListener : function(listener) {
        this.listeners.add(listener);
    },
    setListeners : function(listeners) {
        this.listeners = listeners;
    },
    onContextMenu : function(x, y) {
        $.contextMenu({
            selector : 'body',
            events : {
                hide : function() {
                    $.contextMenu('destroy');
                }
            },
            callback : $.proxy(function(key, options) {
                switch (key) {
                    case "Properties":
                        if(this.contextMenuHandler!=null){
                            if(typeof this.contextMenuHandler=="function")
                                this.contextMenuHandler(this,key);
                        }
                        break;
                    case "Delete":
                        // without undo/redo support
                        // this.getCanvas().remove(this);

                        // with undo/redo support
                        var cmd = new draw2d.command.CommandDelete(this);
                        this.getCanvas().getCommandStack().execute(cmd);
                    default:
                        break;
                }

            }, this),
            x : x,
            y : y,
            items : {
                "Properties" : {
                    name : "Properties"
                },
                "sep1" : "---------",
                "Delete" : {
                    name : "Delete"
                }
            }
        });
    },
    getGeneralXML:function(){
        var name = this.id;
        var moduleName = $.trim(this.name);
        if(moduleName != null && moduleName != "")
            name = moduleName;
        var xml=' id="'+this.id+'" name="'+name+'" ';
        if(this.asynchronous){
            xml=xml+'activiti:async="true" '
        }
        if(!this.exclusive){
            xml=xml+'activiti:exclusive="false" '
        }
        return xml;
    },
    getMultiInstanceXML:function(){
        var xml = '';
        if(this.isSequential){
            xml=xml+'<multiInstanceLoopCharacteristics ';
            if(this._elementVariable!=null&&this._elementVariable!='')
                xml=xml+'activiti:elementVariable="'+this._elementVariable+'" ';
            if(this._collection!=null&&this._collection!='')
                xml=xml+'activiti:collection="'+this._collection+'" ';
            xml=xml+'>\n'
            if(this._loopCardinality!=null&&this._loopCardinality!='')
                xml=xml+'<loopCardinality>'+this._loopCardinality+'</loopCardinality>\n';
            if(this._completionCondition!=null&&this._completionCondition!='')
                xml=xml+'<completionCondition>'+this._completionCondition+'</completionCondition>\n'
            xml=xml+'</multiInstanceLoopCharacteristics>\n';
        }
        return xml;
    },
    getExtensionElementsXML:function(){
        if(this.listeners.getSize()==0)return '';
        var xml = '<extensionElements>\n';
        xml=xml+this.getListenersXML();
        xml=xml+'</extensionElements>\n';
        return xml;
    },
    getListenersXML:function(){
        var xml = '';
        for(var i=0;i<this.listeners.getSize();i++){
            var listener = this.listeners.get(i);
            xml=xml+listener.toXML();
        }
        return xml;
    },
    toXML:function(){
        return "";
    },
    toBpmnDI:function(){
        var w=this.getWidth();
        var h=this.getHeight();
        var x=this.getAbsoluteX();
        var y=this.getAbsoluteY();
        var xml='<bpmndi:BPMNShape bpmnElement="'+this.getId()+'" id="BPMNShape_'+this.getId()+'">\n';
        xml=xml+'<omgdc:Bounds height="'+h+'" width="'+w+'" x="'+x+'" y="'+y+'"/>\n';
        xml=xml+'</bpmndi:BPMNShape>\n';
        return xml;
    }
});
/*
 * Test类型的结点对应的类
 */
ESTDesigner.module.Test = ESTDesigner.module.BaseModule.extend({
    init : function(attr, setter, getter) {
        this._super($.extend({
            type : "ESTDesigner.module.Test",
            title : "",
            iconPath : "js/ESTDesigner/icons/database_50.png"
        }, attr), setter, getter);
        this.performerType=null;
        this.dueDate=null;
        this.priority=null;
        this.formKey = null;
        this.expression=null;
        this.isUseExpression=null;
        this.assignee=null;
        this.candidateUsers=new draw2d.util.ArrayList();
        this.candidateGroups=new draw2d.util.ArrayList();
        this.formProperties=new draw2d.util.ArrayList();
        this.moduleListeners=new draw2d.util.ArrayList();
    },
    getIconClassName:function(){
        return "database-icon";
    },
    getStartElementXML:function(){
        var xml='<Test ';
        xml=xml+this.getGeneralXML();
        xml=xml+this.getPerformersXML();
        xml=xml+'>\n';
        return xml;
    },
    getEndElementXML:function(){
        var xml = '</Test>\n';
        return xml;
    },
    getDocumentationXML:function(){
        if(this.documentation==null||this.documentation=='')return '';
        var xml='<documentation>';
        xml=xml+this.documentation;
        xml=xml+'</documentation>';
        return xml;
    },
    getPerformersXML:function(){
        var xml='';
        if(this.isUseExpression){
            if(this.expression!=null&&this.expression!=''){
                if(this.performerType=='assignee'){
                    xml=xml+'activiti:assignee="'+this.expression+'" ';
                }else if(this.performerType=='candidateUsers'){
                    xml=xml+'activiti:candidateUsers="'+this.expression+'" ';
                }else if(this.performerType=='candidateGroups'){
                    xml=xml+'activiti:candidateGroups="'+this.expression+'" ';
                }
            }
        }else{
            if(this.performerType=='assignee'){
                if(this.assignee!=null&&this.assignee!='')
                    xml=xml+this.assignee;
            }else if(this.performerType=='candidateUsers'){
                for(var i=0;i<this.candidateUsers.getSize();i++){
                    var user = this.candidateUsers.get(i);
                    xml=xml+user.sso+',';
                }
            }else if(this.performerType=='candidateGroups'){
                for(var i=0;i<this.candidateGroups.getSize();i++){
                    var group = this.candidateGroups.get(i);
                    xml=xml+group+',';
                }
            }
        }
        if(this.dueDate!=null&&this.dueDate!=''){
            xml=xml+'activiti:dueDate="'+this.dueDate+'" '
        }
        if(this.formKey != null && this.formKey != ""){
            xml=xml+'activiti:formKey="'+this.formKey+'" ';
        }
        if(this.priority!=null&&this.priority!=''){
            xml=xml+'activiti:priority="'+this.priority+'" '
        }

        return xml;
    },
    getExtensionElementsXML:function(){
        if(this.listeners.getSize()==0&&this.formProperties.getSize()==0)return '';
        var xml = '<extensionElements>\n';
        xml=xml+this.getFormPropertiesXML();
        xml=xml+this.getListenersXML();
        xml=xml+'</extensionElements>\n';
        return xml;
    },
    getListenersXML:function(){
        var xml = this._super();
        for(var i=0;i<this.moduleListeners.getSize();i++){
            var listener = this.moduleListeners.get(i);
            xml=xml+listener.toXML();
        }
        return xml;
    },
    getFormPropertiesXML:function(){
        var xml = '';
        for(var i=0;i<this.formProperties.getSize();i++){
            var formProperty = this.formProperties.get(i);
            xml=xml+formProperty.toXML();
        }
        return xml;
    },
    toXML:function(){
        var xml=this.getStartElementXML();
        xml=xml+this.getDocumentationXML();
        xml=xml+this.getExtensionElementsXML();
        xml=xml+this.getMultiInstanceXML();
        xml=xml+this.getEndElementXML();
        return xml;
    }
});
